package com.cnebula.uas.sso.controller;

import javax.servlet.http.HttpServletResponse;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.apache.http.client.utils.URIBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.cnebula.uas.sso.ConfAdmin;
import com.cnebula.uas.sso.model.IdpAuthCenter;
import com.cnebula.uas.sso.service.IdpAuthCenterService;
import com.cnebula.uas.sso.service.IdpLoginValidService;

@Controller
@RequestMapping(value = "/delegate")
public class IDPDelegateController {

	Logger log = LoggerFactory.getLogger(IDPDelegateController.class);

	@Autowired
	ConfAdmin conf;

	@Autowired
	IdpAuthCenterService idpAuthCenterService;

	@Autowired
	IdpLoginValidService idpLoginValidService;

	/**
	 * idp认证
	 */
	@RequestMapping(value = "/idp/{idpCode}")
	@GetMapping
	public void idp(@PathVariable String idpCode, @RequestParam String sp, @RequestParam(value = "goto") String gotoURL,
			HttpServletRequest request, HttpServletResponse response) throws Exception {

		IdpAuthCenter idpAuthCenter = idpAuthCenterService.getIdpAuthCenter(idpCode);

		String authURL = idpAuthCenter.authURL;

		String callbackURL = "http://" + conf.host + ":" + conf.port + "/delegate/idp/" + idpCode + "/callback?verb=cb";

		URIBuilder uasCallbackURLBuilder = new URIBuilder(sp);
		uasCallbackURLBuilder.addParameter("goto", gotoURL);
		uasCallbackURLBuilder.addParameter("idp", idpCode);

		URIBuilder authURLBuilder = new URIBuilder(authURL);
		authURLBuilder.addParameter("sp", callbackURL);
		authURLBuilder.addParameter("goto", uasCallbackURLBuilder.build().toString());
		authURLBuilder.addParameter("idp", idpCode);

		String url = authURLBuilder.build().toString();

		log.info(url);

		response.sendRedirect(url);

	}

	/**
	 * idp认证后的回调服务
	 */
	@RequestMapping(value = "/idp/{idpCode}/callback")
	@GetMapping
	public void callback(@PathVariable String idpCode, @RequestParam String artifact,
			@RequestParam(value = "goto") String gotoURL, HttpServletResponse response) throws Exception {

		URIBuilder uasCallbckURLBuilder = new URIBuilder(gotoURL);
		uasCallbckURLBuilder.addParameter("artifact", artifact);
		
		String url = uasCallbckURLBuilder.build().toString();
		
		log.info(url);
		
		response.sendRedirect(url);
	}

	/**
	 * 验证artifact
	 */
	@RequestMapping(value = "/idp/{idpCode}/valid")
	@GetMapping
	public ResponseEntity<String> loginValid(@PathVariable String idpCode, @RequestParam String artifact) throws Exception {

		String userInfo = idpLoginValidService.valid(idpCode, artifact);

		return ResponseEntity.ok(userInfo);
	}
}
